#!/usr/bin/env python
# encoding: utf-8
"""File: sidebar_gallery.py.

Author: Florian Wagner <fwagner@gfz-potsdam.de>
Description: Add all examples/tutorials to sidebar gallery
Created on: 2013-10-13
"""

import os
import random
import re
from glob import glob
from os.path import basename, dirname, join

import pygimli as pg

def make_gallery(src_path, out_path):
    """TODO DOCUMENTME."""
    pg.info(f"Creating gallery: {src_path} at: {out_path}")
    example_dir = join(src_path, "examples")
    tutorial_dir = join(src_path, "tutorials")
    img_dir = "{{ pathto('_images', 1) }}"

    # Get examples/tutorials
    examples = [fn for fn in glob(join(example_dir, "*/*plot*.py"))
                if not join(example_dir, "dev") in fn]
    tutorials = [fn for fn in glob(join(tutorial_dir, "*/*plot*.py"))
                 if not join(tutorial_dir, "dev") in fn]

    # Get captions
    def readRSTSecTitles(fname, verbose=False):
        """ Return list of section titles found in a given RST file. """
        print('fname:', fname)
        rst_titles = re.compile(r"^(.+)\n[-=]+\n", flags=re.MULTILINE)
        
        with open(fname, encoding="utf8") as f:

            titles = re.findall(rst_titles, f.read())
            if verbose:
                print(("File:", fname))
                print(("Title:", titles))

        # go through lines only if compiled regex fails (py2/py3 issue)
        if not titles:
            print(("WARNING: Problem reading section title in", fname))
            with open(fname, encoding="utf8") as f:
                title = "unknown"
                for line in f.readlines():
                    if "---" in line or "===" in line:
                        titles.append(title)  # add the line after
                    title = line

        return titles[0].rstrip()

    ex_titles = [readRSTSecTitles(ex) for ex in examples]
    tut_titles = [readRSTSecTitles(tut) for tut in tutorials]
    titles = ex_titles + tut_titles

    # Adjust paths to output directory for html links
    examples = [e.replace(example_dir, "{{ pathto('_examples_auto', 1) }}")
                for e in examples]

    tutorials = [t.replace(tutorial_dir, "{{ pathto('_tutorials_auto', 1) }}")
                 for t in tutorials]

    # Create HTML gallery for sidebar with random start item
    gallery = examples + tutorials
    print(("\nAdding %d examples/tutorials to sidebar gallery.\n" %
          len(gallery)))
    print(("\t{:40}{}\n\t".format("Title", "File") + "-" * 80))
    for line in zip(titles, gallery):
        print(("\t{:40}{}".format(*line)))
    print("\n")

    html_top = """\
<!-- This file is automatically generated by sidebar_gallery.py -->
          <div id="sidebar_example_gallery" class="carousel slide">
            <div class="carousel-inner">"""

    html_bottom = """\
            </div>
            <button class="carousel-control-prev" type="button" data-bs-target="#sidebar_example_gallery" data-bs-slide="prev">
              <span class="carousel-control-prev-icon" aria-hidden="true"></span>
              <span class="visually-hidden">Previous</span>
            </button>
            <button class="carousel-control-next" type="button" data-bs-target="#sidebar_example_gallery" data-bs-slide="next">
              <span class="carousel-control-next-icon" aria-hidden="true"></span>
              <span class="visually-hidden">Next</span>
            </button>
          </div>"""

    html_item = """\
              <div class="carousel-item">
                <a href="{}">
                  <img src="{}">
                  <div class="carousel-caption d-none d-md-block">
                    <h6>{}</h6>
                  </div>
                </a>
              </div>"""

    idx = random.randint(0, len(gallery) - 1)
    items = []
    for ix, (item, title) in enumerate(zip(gallery, titles)):
        path = dirname(item)
        name = basename(item)
        url = join(path, name.replace(".py", ".html"))
        img = join(img_dir, "sphx_glr_" + name.replace(".py", "_thumb.png"))
        item = html_item.format(url, img, title)

        if ix == idx:
            item = item.replace("item", "item active")

        items.append(item)

    items.insert(0, html_top)
    items.append(html_bottom)
    html = "\n".join(items)

    if not os.path.exists(join(out_path, "_templates/")):
        os.mkdir(join(out_path, "_templates/"))

    with open(join(out_path, "_templates/gallery.html"), "w") as file:
        file.write(html)


if __name__ == '__main__':
    make_gallery('.', '.')
